package com.stu.manager.studentscoremanagersys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stu.manager.studentscoremanagersys.Utils.PageUtils;
import com.stu.manager.studentscoremanagersys.exception.BIZexception;
import com.stu.manager.studentscoremanagersys.pojo.Student;
import com.stu.manager.studentscoremanagersys.req.ComonListRequest;
import com.stu.manager.studentscoremanagersys.req.ReqStudentAdd;
import com.stu.manager.studentscoremanagersys.req.ReqStudentUpdate;
import com.stu.manager.studentscoremanagersys.res.Result;
import com.stu.manager.studentscoremanagersys.service.StudentService;
import com.stu.manager.studentscoremanagersys.mapper.StudentMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author 86135
* @description 针对表【student】的数据库操作Service实现
* @createDate 2022-11-03 19:26:27
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService{

    @Autowired
    StudentMapper studentMapper;

    /**
     * 添加学生
     * @param reqStudentAdd
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(ReqStudentAdd reqStudentAdd) {
        LambdaQueryWrapper<Student> lambda = new LambdaQueryWrapper<>();
        lambda.eq(Student::getNo,reqStudentAdd.getNo());
        Long count = studentMapper.selectCount(lambda);
        if (count > 0){
            throw new BIZexception("学号已经存在");
        }
        Student student = new Student();
        BeanUtils.copyProperties(reqStudentAdd,student);
        studentMapper.insert(student);
    }

    /**
     * 分页查询
     * @param commonVo
     * @return
     */
    @Override
    public Result<List<Student>> selectPage(ComonListRequest commonVo) {
        PageUtils.startPage(commonVo.getPageNow(),commonVo.getPageSize());
        List<Student> list= studentMapper.queryList(commonVo);
        return PageUtils.pageList(list);
    }

    /**
     * 删除学生
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Long id) {
        studentMapper.deleteById(id);
    }

    /**
     * 查询学生信息
     * @param id
     * @return
     */
    @Override
    public Result selectById(Long id) {
        LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Student::getId,id);
        Student student = studentMapper.selectOne(lambdaQueryWrapper);
        if (student == null){
            return Result.buildEmptyResult();
        }
        return Result.buildScuess(student);
    }

    /**
     * 更新学生信息
     * @param update
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<Student> updateById(ReqStudentUpdate update) {
        Student student = new Student();
        BeanUtils.copyProperties(update,student);
        studentMapper.updateById(student);
        return Result.buildScuess(student);
    }

    /**
     * 查询所有学生
     * @return
     */
    @Override
    public Result<List<Student>> selectAll() {
        List<Student> list = studentMapper.queryList(new ComonListRequest());
        return Result.buildScuess(list);
    }
}




